package code.Frame.userFrame;

import code.Frame.EPFrame.DIinput;
import code.Frame.FrameManager;
import code.Main;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static code.Main.WINDOW_HEIGHT;
import static code.Main.WINDOW_WIDTH;

public class mainFrame {
    public JPanel panel;
    private JButton info;
    private JButton query_order;
    private JButton order;
    private JButton back;
    private JLabel title;

    public mainFrame() {
        back.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                Main.frame.remove(Main.panel_now);
                Main.panel_now = FrameManager.startPanel;
                Main.frame.setContentPane(Main.panel_now);
                Main.panel_now.revalidate();
            }
        });
        info.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                Main.frame.remove(Main.panel_now);
                Main.panel_now = FrameManager.user_info;
                FrameManager.user_infoFrame.refresh_list();
                Main.frame.setContentPane(Main.panel_now);
                Main.panel_now.revalidate();
            }
        });
        query_order.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                Main.frame.remove(Main.panel_now);
                Main.panel_now = FrameManager.user_query;
                FrameManager.user_queryFrame.refresh_table();
                FrameManager.user_queryFrame.start_auto_fresh();
                Main.frame.setContentPane(Main.panel_now);
                Main.panel_now.revalidate();
            }
        });
        order.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                JFrame frame = new JFrame("下单");
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                frame.setBounds(((int) screenSize.getWidth() - WINDOW_WIDTH) / 2, ((int) screenSize.getHeight() - WINDOW_HEIGHT) / 2,
                        WINDOW_WIDTH, WINDOW_HEIGHT);
                orderFrame orderFrame = new orderFrame();
                frame.setContentPane(orderFrame.panel);
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run()
                    {
                        while (!orderFrame.end)
                        {
                            try {
                                Thread.sleep(200);
                            } catch (InterruptedException ex) {
                                throw new RuntimeException(ex);
                            }
                        }
                        frame.dispose();
                    }
                });
                thread.start();
            }
        });
    }
}
